#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <string>
#include <sys/types.h>      
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include "server_func.h"

using namespace std;

int epor(int sockfd, int connfd)//epoll机制，实现IO多路复用的并发服务器	
{		
	int epfd = epoll_create(128);
	if(epfd == -1)
	{
		perror("epoll_create");
		exit(-1);
	}

	struct epoll_event ev, evs[128];

	ev.events = EPOLLIN;
	ev.data.fd = sockfd;

	epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);

	char buf[64] = {0};

	int m, i;

	cout << "等待客户端连接"<< endl;

	int stat = 0;
	while(1)
	{
		
		m = epoll_wait(epfd, evs, 128, -1);
		if(m == -1)
			
		{
			perror("epoll_wait");
			exit(-1);
		}
		
	
		for(i=0 ; i<m; i++)
		{
			if(evs[i].events & EPOLLIN)
			{
				int fd = evs[i].data.fd;
				
				if(fd == sockfd)
				{
					connfd = connect(sockfd);//创建监听、通信套接字
					
					ev.events = EPOLLIN;
					ev.data.fd = connfd;

					epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev);
					
					send_video();
					
					continue;
				}
				
				else
				{	
					//视频传输
					//send_video();
					stat = -1;
					break;
				}
			}		
		}
		
		if(stat == -1)
		{
			break;
		}
		
	}
	
	return 0;

}
